import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:hc_bot_kit/state.dart';

import 'hc_bot_kit_platform_interface.dart';

/// An implementation of [HcBotKitPlatform] that uses method channels.
class MethodChannelHcBotKit extends HcBotKitPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('hc_bot_kit');

  MethodChannelHcBotKit() {
    methodChannel.setMethodCallHandler((call) async {
      debugPrint(call.method);
      switch (call.method) {
        case "onNavStateChanged":
          debugPrint("onNavStateChanged:${call.arguments}");
          final state = GuideState.fromString(call.arguments.toString());
          controller?.add(state);
          break;
        default:
          break;
      }
    });
  }

  @override
  Future connect(String ipAddress) async {
    await super.connect(ipAddress);
    final ret = await methodChannel.invokeMethod<bool>('connect', {
      'ipAddress': ipAddress,
    });
    if (true != ret) {
      return Future.error('连接失败');
    }
  }

  @override
  Future<bool> guide(String destination) async {
    final ret = await methodChannel.invokeMethod<bool>('guide', {
      'destination': destination,
    });
    return ret ?? false;
  }

  @override
  Future close() {
    return methodChannel.invokeMethod('close');
  }

  @override
  Future<List<String>> fetchAllPoints() async {
    final ret = await methodChannel.invokeMethod<List>('fetchAllPoints') ?? [];
    return List<String>.from(ret);
  }
}
